import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.less';
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
import { checkToken, success_code } from './api/api';
Vue.use(Antd);
Vue.use(mavonEditor);
Vue.config.productionTip = false
//全局路由守卫

router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    next();
  } else {
    //检查用户角色
    checkToken().then(result => {
      if (result.code === 200) {

        //成功，判断用户角色
        if (result.data.roles === 'role_admin') {
          window.localStorage.setItem('avatar', result.data.avatar);
          window.localStorage.setItem('userName', result.data.userName);
          next();
        } else {
          //跳转到首页
          location.href = 'https://60.205.178.38:8000';
        }
      } else {
        //跳转到登录界面
        next({
          path: '/login'
        });
      }
    }).catch(res => {
      next({
        path: '/login'
      });
    })
  }
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
